﻿using System;
using System.Reflection;
using System.Windows;

namespace TinyEngine.Editor.Themes
{
    public static class Manager
    {
        private static ResourceDictionary ThemeDict(string name)
        {
            if (string.IsNullOrEmpty(name))
                return null;
            
            Assembly.LoadFrom("TinyEngine.Editor.Themes.dll");
            string themePath = String.Format(@"/TinyEngine.Editor.Themes;component/{0}.xaml", name);
            Uri themeUri = new Uri(themePath, UriKind.Relative);

            return Application.LoadComponent(themeUri) as ResourceDictionary;
        }

        public static void ApplyTheme(this Application app, string theme)
        {
            ResourceDictionary dictionary = Manager.ThemeDict(theme);

            if (dictionary == null)
                return;

            if (!app.Resources.MergedDictionaries.Contains(dictionary))
                app.Resources.MergedDictionaries.Add(dictionary);
        }

        public static void RemoveTheme(this Application app, string theme)
        {
            ResourceDictionary dictionary = Manager.ThemeDict(theme);

            if (dictionary == null)
                return;

            if (app.Resources.MergedDictionaries.Contains(dictionary))
                app.Resources.MergedDictionaries.Remove(dictionary);
        }
    }
}
